package com.voja.salonschedule;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.view.menu.MenuBuilder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LandscapeMenuAdapter extends ArrayAdapter<LandscapeMenuAdapter.Item> {

    ArrayList<LandscapeMenuAdapter.Item> data = new ArrayList<LandscapeMenuAdapter.Item>();
    int resource;
    
    ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();
    
    class Item {
        int id;
        String title;
        Drawable icon;
        
        public String toString() {
            return id+". "+title;
        }
    };
    
    public LandscapeMenuAdapter(Context context, int resource) {
        super(context, resource);
        
        this.resource = resource;
        
        loadMenuItems();
        
        Activity activity = ((Activity)context);
        ListView leftList = (ListView)activity.findViewById(R.id.leftMenu);
        leftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItem menuItem = menuItems.get(position);
                ((Activity)getContext()).onOptionsItemSelected(menuItem);
            }
        });
        
        leftList.setAdapter(this);
    }
    
    public void loadMenuItems() {
        Menu menu = new MenuBuilder(getContext());
        ((Activity)getContext()).getMenuInflater().inflate(R.menu.index, menu);
        
        MenuItem menuItem;
        Item item;
        for (int i=0; i<menu.size(); i++) {
            menuItem = menu.getItem(i);
            if (menuItem.getItemId() != R.id.menuNextDate && menuItem.getItemId() != R.id.menuPrevDate) {
                item       = new Item();
                item.id    = menuItem.getItemId();
                item.title = menuItem.getTitle().toString();
                item.icon  = menuItem.getIcon();
                
                data.add(item);
                menuItems.add(menuItem);
            }
        }
    }
    
    @Override
    public Item getItem(int index) {
        return data.get(index);
    }
    
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public View getView(int index, View convertView, ViewGroup parent) {
        Item item = getItem(index);
        
        if (convertView == null) {
            convertView = ((Activity)getContext()).getLayoutInflater().inflate(resource, parent, false);
            convertView.setId(item.id);
        }
        
        TextView label = (TextView)convertView.findViewById(R.id.menuItemLabel);
        label.setText(item.title);
        
        ImageView icon = (ImageView)convertView.findViewById(R.id.menuItemIcon);
        icon.setImageDrawable(item.icon);
        
        return convertView;
    }

}
